from fastapi import FastAPI, Query
import uvicorn

# 创建app对象
app = FastAPI()

# 测试数据
books = [
    {"id": 1, "title": "看漫画学Python", "author": "关东升", "price": 29.99},
    {"id": 2, "title": "杀死一只知更鸟", "author": "Harper Lee", "price": 19.99},
    {"id": 3, "title": "Python从小白到大牛", "author": "关东升", "price": 49.99},
    {"id": 4, "title": "漫画Java", "author": "关东升", "price": 39.99},
]


# 路由函数
@app.get("/books/")
async def read_book(start: int, end: int):
    return books[start : start + end]


# 路由函数
@app.get("/books2/")  # 使用Query函数
async def read_book(
    start: int = Query(0, alias="start-index", description="这是开始参数", lt=4),
    end: int = Query(10, alias="end-index", gt=3),
):
    return books[start : start + end]


if __name__ == "__main__":
    uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)
